//
//  ShareUserCell.m
//  SenMo
//
//  Created by ZengYuan on 15/12/23.
//  Copyright © 2015年 ZengYuan. All rights reserved.
//

#import "ShareUserCell.h"
#import "UserUnit.h"
#import "ShareUserVIew.h"
#import "Config.h"






@implementation ShareUserCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setUserArray:(NSArray *)auserArray{
    
    for (UIView *subview in self.subviews) {
        [subview removeFromSuperview];
    }
    
    _userArray = auserArray;
    
    NSInteger rows = auserArray.count>0?((_userArray.count-1)/USERCOUNT_ROW +  1 ): 1;

    
    float left_right_x = 15;
    float intervalX =(kScreenWidth - left_right_x*2 - USERCOUNT_ROW *ShareUser_Width) / (USERCOUNT_ROW - 1);
    float intervalY = 10;

    showItemArray = [NSMutableArray arrayWithArray:_userArray];
    UserUnit *userunit = [[UserUnit alloc] init];
    userunit.headImgUrl=@"加号";
    userunit.useruuid = @"添加";
    [showItemArray addObject:userunit];
    
    userunit = [[UserUnit alloc] init];
    userunit.headImgUrl=@"减号";
    userunit.useruuid = @"删除";

    [showItemArray addObject:userunit];
    
    for (int i=0; i<showItemArray.count; i++) {
        
        UserUnit *user = showItemArray[i];
        
        CGFloat x = left_right_x + (i%USERCOUNT_ROW)*intervalX + i%USERCOUNT_ROW * ShareUser_Width;
        
        ShareUserVIew *suv = [[ShareUserVIew alloc]initWithFrame:
                              CGRectMake(x,
                                         (i/USERCOUNT_ROW+1)*intervalY + i/USERCOUNT_ROW * ShareUser_Height
                                         , ShareUser_Width, ShareUser_Height )];
        suv.user = user;
        
        [self addSubview:suv];
        suv.tag = 500+i;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [suv addGestureRecognizer:tap];
        tap.numberOfTapsRequired=1;
        
        
    }
    
    
    
}

-(void)tap:(UITapGestureRecognizer *)sender{
    NSInteger  index =  sender.view.tag-500;
    UserUnit *user = showItemArray[index];
    self.tapBlock(user, index);
}



@end
